import axios from '@/libs/api.request'

export function lenValid(str, num, callback) {
  if (str) {
    let len = str.length
    if (len > num) {
      return callback(new Error('数据长度过长，请重新输入'))
    }
  }
  return callback()
}
export function checkCardNo (rule, value, callback) {
  // 身份证号
  const regIdCard = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/
  if (!value) {
    return callback(new Error('必须填写证件号码'))
  } else if (!regIdCard.test(value)) {
    return callback(new Error('身份证号格式错误'))
  } else {
    return callback()
  }
}

export function IdentityCodeValid(rule, code, callback) {
  var city = {
    11: '北京',
    12: '天津',
    13: '河北',
    14: '山西',
    15: '内蒙古',
    21: '辽宁',
    22: '吉林',
    23: '黑龙江 ',
    31: '上海',
    32: '江苏',
    33: '浙江',
    34: '安徽',
    35: '福建',
    36: '江西',
    37: '山东',
    41: '河南',
    42: '湖北 ',
    43: '湖南',
    44: '广东',
    45: '广西',
    46: '海南',
    50: '重庆',
    51: '四川',
    52: '贵州',
    53: '云南',
    54: '西藏 ',
    61: '陕西',
    62: '甘肃',
    63: '青海',
    64: '宁夏',
    65: '新疆',
    71: '台湾',
    81: '香港',
    82: '澳门',
    91: '国外 '
  };

  var reg = /^\d{6}(18|19|20)?\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}(\d|X|x)$/i;

  if (!code || !reg.test(code)) {
    return callback(new Error('身份证号格式错误'))
  } else if (!city[code.substr(0, 2)]) {
    return callback(new Error('身份证号格式错误'))
  } else {
    // 18位身份证需要验证最后一位校验位
    if (code.length == 18) {
      code = code.split('');
      // ∑(ai×Wi)(mod 11)
      // 加权因子
      var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
      // 校验位
      var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
      var sum = 0;
      var ai = 0;
      var wi = 0;
      for (var i = 0; i < 17; i++) {
        ai = code[i];
        wi = factor[i];
        sum += ai * wi;
      }
      if (parity[sum % 11] != code[17].toUpperCase()) {
        return callback(new Error('身份证号格式错误'))
      }
    }
  }
  return callback();
}

export function DateFormat (date, format) {
  if (!date) {
    return ''
  }
  let d = new Date(date)
  // 年
  if (/YYYY/.test(format)) {
    format = format.replace(/YYYY/, d.getFullYear())
  }
  // 月份
  let month = d.getMonth() + 1
  if (/MM/.test(format)) {
    let monthStr = month < 10 ? '0' + month : month
    format = format.replace(/MM/, monthStr)
  } else if (/M/.test(format)) {
    format = format.replace(/M/, month)
  }
  // 日期
  let dates = d.getDate()
  if (/DD/.test(format)) {
    let dateStr = dates < 10 ? '0' + dates : dates
    format = format.replace(/DD/, dateStr)
  } else if (/D/.test(format)) {
    format = format.replace(/D/, dates)
  }
  // 小时
  let hours = d.getHours()
  if (/HH/.test(format)) {
    let hoursStr = hours < 10 ? '0' + hours : hours
    format = format.replace(/HH/, hoursStr)
  } else if (/H/.test(format)) {
    format = format.replace(/H/, hours)
  } else if (/hh/.test(format)) {
    let hoursMin = hours > 12 ? hours - 12 : hours
    let hoursStr = hoursMin < 10 ? '0' + hoursMin : hoursMin
    format = format.replace(/hh/, hoursStr)
  } else if (/h/.test(format)) {
    let hoursMin = hours > 12 ? hours - 12 : hours
    format = format.replace(/h/, hoursMin)
  }
  // 分
  let minutes = d.getMinutes()
  if (/mm/.test(format)) {
    let minutesStr = minutes < 10 ? '0' + minutes : minutes
    format = format.replace(/mm/, minutesStr)
  } else if (/m/.test(format)) {
    format = format.replace(/m/, minutes)
  }
  // 秒
  let seconds = d.getSeconds()
  if (/ss/.test(format)) {
    let secondsStr = seconds < 10 ? '0' + seconds : seconds
    format = format.replace(/ss/, secondsStr)
  } else if (/s/.test(format)) {
    format = format.replace(/s/, seconds)
  }
  return format
}

// 时间转换为日期-> 2019-02-02
export function date2str (date, containtime) {
  if (!date || typeof date === 'string') {
    return date
  }
  var time1 = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  time1 += '-' + (month < 10 ? '0' + month : month)
  time1 += '-' + (day < 10 ? '0' + day : day)
  return time1
}

// 生成uuid
export function getUuid () {
  var s = []
  var hexDigits = '0123456789abcdef'
  for (var i = 0; i < 36; i++) {
    s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
  }
  s[14] = '4' // bits 12-15 of the time_hi_and_version field to 0010
  s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1) // bits 6-7 of the clock_seq_hi_and_reserved to 01
  s[8] = s[13] = s[18] = s[23] = '-'
  var uuid = s.join('')
  return uuid
}

export function date3str (t, date) {
  var date2 = new Date(date)
  var o = {
    'M+': date2.getMonth() + 1,
    'd+': date2.getDate(),
    'h+': date2.getHours(),
    'm+': date2.getMinutes(),
    's+': date2.getSeconds(),
    'q+': Math.floor((date2.getMonth() + 3) / 3),
    'S': date2.getMilliseconds()
  }
  if (/(y+)/.test(t)) {
    t = t.replace(RegExp.$1, (date2.getFullYear() + '').substr(4 - RegExp.$1.length))
  }
  for (var k in o) {
    if (new RegExp('(' + k + ')').test(t)) {
      t = t.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
    }
  }
  return t
}

export function date0str (date, containtime) {
  if (!date || typeof date === 'string') {
    return date
  }
  var time1 = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()
  var hour = date.getHours()
  var min = date.getMinutes()
  time1 += '-' + (month < 10 ? '0' + month : month)
  time1 += '-' + (day < 10 ? '0' + day : day)
  time1 += ' ' + (hour < 10 ? '0' + hour : hour)
  time1 += ':' + (min < 10 ? '0' + min : min)
  return time1
}

// 时间转字符串 年月日时分秒
export function datetime2str (date, containtime) {
  let time1 = date.getFullYear() + ''
  let month = date.getMonth() + 1
  let day = date.getDate()
  let hh = date.getHours()
  let mm = date.getMinutes()
  let ss = date.getSeconds()
  time1 += month < 10 ? '0' + month : month
  time1 += day < 10 ? '0' + day : day
  time1 += hh < 10 ? '0' + hh : hh
  time1 += mm < 10 ? '0' + mm : mm
  time1 += ss < 10 ? '0' + ss : ss
  return time1
}

// 获取列表
export const getPageList = (data) => {
  return axios.request({
    url: '/api/ac/cscforxq/identityInformaitonService/getPageList',
    method: 'post',
    data: data
  })
}
// 新增
export const addData = (data) => {
  return axios.request({
    url: '/api/ac/cscforxq/identityInformaitonService/addData',
    method: 'post',
    data: data
  })
}
// 修改
export const updateData = (data) => {
  return axios.request({
    url: '/api/ac/cscforxq/identityInformaitonService/updateData',
    method: 'post',
    data: data
  })
}
// 删除
export const deleteData = (data) => {
  return axios.request({
    url: '/api/ac/cscforxq/identityInformaitonService/deleteData',
    method: 'post',
    data: data
  })
}
export function postAction(url, parameter) {
  return axios.request({
    url: url,
    type: 'post',
    data: parameter
  })
}
